package com.lechi.web.service.impl;

import com.lechi.web.common.enums.ErrorCodeEnum;
import com.lechi.web.common.exception.ServiceException;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.service.IIotPublishAppService;
import com.lechi.web.system.domain.IotPublishApp;
import com.lechi.web.system.mapper.iot.IotPublishAppMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 内容发布管理Service业务层处理
 *
 * @author yangguang
 * @date 2024 -07-10
 */
@Service
@RequiredArgsConstructor
public class IotPublishAppServiceImpl implements IIotPublishAppService {

    private final IotPublishAppMapper iotPublishAppMapper;

    /**
     * 查询内容发布管理
     *
     * @param id 内容发布管理主键
     * @return 内容发布管理
     */
    @Override
    public IotPublishApp selectIotPublishAppById(Long id) {
        return iotPublishAppMapper.selectIotPublishAppById(id);
    }

    /**
     * 查询内容发布管理列表
     *
     * @param iotPublishApp 内容发布管理
     * @return 内容发布管理
     */
    @Override
    public List<IotPublishApp> selectIotPublishAppList(IotPublishApp iotPublishApp) {
        try {
            return iotPublishAppMapper.selectIotPublishAppList(iotPublishApp);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "查询内容发布管理列表", method, iotPublishApp));
            throw new ServiceException(ErrorCodeEnum.QUERY_ERROR.getValue(), ErrorCodeEnum.QUERY_ERROR.getKey());
        }
    }

    /**
     * 新增内容发布管理
     *
     * @param iotPublishApp 内容发布管理
     * @return 结果
     */
    @Override
    public int insertIotPublishApp(IotPublishApp iotPublishApp) {
        iotPublishApp.setCreateTime(new Date());
        try {
            return iotPublishAppMapper.insertIotPublishApp(iotPublishApp);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "新增内容发布管理", method, iotPublishApp));
            throw new ServiceException(ErrorCodeEnum.ADD_ERROR.getValue(), ErrorCodeEnum.ADD_ERROR.getKey());
        }
    }

    /**
     * 修改内容发布管理
     *
     * @param iotPublishApp 内容发布管理
     */
    @Override
    public void updateIotPublishApp(IotPublishApp iotPublishApp) {
        try {
            iotPublishAppMapper.updateIotPublishApp(iotPublishApp);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "修改内容发布管理", method, iotPublishApp));
            throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
        }
    }

    /**
     * 批量删除内容发布管理
     *
     * @param ids 需要删除的内容发布管理主键
     */
    @Override
    public void deleteIotPublishAppByIds(Long[] ids) {
        try {
            iotPublishAppMapper.deleteIotPublishAppByIds(ids);
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "批量删除内容发布管理", method, ids));
            throw new ServiceException(ErrorCodeEnum.EDIT_ERROR.getValue(), ErrorCodeEnum.EDIT_ERROR.getKey());
        }
    }
}
